package com.yunxi.service.dubboService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.web.DictConstant;
import com.yunxi.model.sto.PartnerFinanceRelation;
import com.yunxi.service.localService.sto.PartnerFinanceRelationService;
import com.yunxi.sto.pojo.PartnerFinanceRelationAccountVO;
import com.yunxi.sto.pojo.PartnerFinanceRelationInfoVO;
import com.yunxi.sto.pojo.PartnerFinanceRelationVO;
import com.yunxi.sto.service.PartnerFinanceRelationDubboService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

/**
 * <p>
 * 渠道商融资机构关系 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-09-17
 */
public class PartnerFinanceRelationDubboServiceImpl implements PartnerFinanceRelationDubboService {

    @Autowired
    @Qualifier(value = "partnerFinanceRelationServiceImpl")
    PartnerFinanceRelationService partnerFinanceRelationService;


    @Override
    public PartnerFinanceRelationVO getById(String id) throws Exception {
        PartnerFinanceRelation partnerFinanceRelation = partnerFinanceRelationService.getById(id);
        if(partnerFinanceRelation == null) throw new Exception("操作失败,无法根据主键:"+id+"找到对应记录!");
        PartnerFinanceRelationVO partnerFinanceRelationVO = new PartnerFinanceRelationVO();
        BeanUtils.copyProperties(partnerFinanceRelation, partnerFinanceRelationVO);
        return partnerFinanceRelationVO;
    }

    @Override
    public void saveOrUpdate(PartnerFinanceRelationVO partnerFinanceRelationVO) throws Exception {
        //校验渠道-付款方式是否唯一
        checkOnlyOne(partnerFinanceRelationVO);
        PartnerFinanceRelation partnerFinanceRelation = new PartnerFinanceRelation();
        BeanUtils.copyProperties(partnerFinanceRelationVO, partnerFinanceRelation);
        partnerFinanceRelationService.saveOrUpdate(partnerFinanceRelation);
    }

    /**
     * 校验渠道-付款方式是否唯一
     * @param partnerFinanceRelationVO
     * @throws Exception
     */
    private void checkOnlyOne(PartnerFinanceRelationVO partnerFinanceRelationVO) throws Exception {
        String partnerCode = partnerFinanceRelationVO.getPartnerCode();
        String payway = partnerFinanceRelationVO.getPayway();
        String relationId = partnerFinanceRelationVO.getRelationId();
        QueryWrapper<PartnerFinanceRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARTNER_CODE",partnerCode);
        queryWrapper.eq("PAYWAY",payway);
        queryWrapper.eq("IS_ON_USE",1);
        if(StringUtils.isNotEmpty(relationId)){
            queryWrapper.ne("RELATION_ID",relationId);
        }
        List<PartnerFinanceRelation> list = partnerFinanceRelationService.list(queryWrapper);
        if(list != null && list.size() > 0)throw new Exception("操作失败,渠道商付款方式已存在!");
    }

    @Override
    public void removeById(String id) {
        partnerFinanceRelationService.removeById(id);
    }

    @Override
    public PageInfo<PartnerFinanceRelationVO> findPageByVO(PartnerFinanceRelationVO partnerFinanceRelationVO, Integer pageNum, Integer pageSize) throws Exception {
        PageInfo<PartnerFinanceRelationVO> pageInfo = partnerFinanceRelationService.pageByVo(partnerFinanceRelationVO, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 导出
     * @param partnerFinanceRelationVO
     * @return
     * @throws Exception
     */
    @Override
    public List<PartnerFinanceRelationVO> export(PartnerFinanceRelationVO partnerFinanceRelationVO) throws Exception {
        return partnerFinanceRelationService.export(partnerFinanceRelationVO);
    }

    @Override
    public void removeByListVo(List<PartnerFinanceRelationVO> partnerFinanceRelationVOList) throws Exception {
        partnerFinanceRelationService.removeByListVo(partnerFinanceRelationVOList);
    }

    @Override
    public List listByVo(PartnerFinanceRelationVO partnerFinanceRelationVO) {
        return partnerFinanceRelationService.listByVo(partnerFinanceRelationVO);
    }

    /**
     * 删除恢复
     * @param list
     * @throws Exception
     */
    @Override
    public void delAndReset(List<String> list,Integer isOnUse) throws Exception {
        if (list == null || list.size() <= 0)throw new Exception("操作失败,list为空!");
        for(String id : list){
            PartnerFinanceRelation partnerFinanceRelation = partnerFinanceRelationService.getById(id);
            if(partnerFinanceRelation != null){
                partnerFinanceRelation.setIsOnUse(isOnUse);
                partnerFinanceRelationService.saveOrUpdate(partnerFinanceRelation);
            }
        }
    }

    /**
     * 导入
     * @param list
     * @throws Exception
     */
    @Override
    public void importSave(List<PartnerFinanceRelationVO> list) throws Exception {
        if(list == null || list.size() <= 0)throw new Exception("操作失败,list为空!");
        for(PartnerFinanceRelationVO vo : list){
            //校验参数
            String companyName = vo.getCompanyName();//金融机构名称
            String partnerName = vo.getPartnerName();//渠道商名称
            String payway = vo.getPayway();//付款方式
            String receiverName = vo.getReceiverName();//联系人姓名
            String receiverPhone = vo.getReceiverPhone();//联系人电话
            String isOnVehicle = vo.getIsOnVehicle();//合格证是否随车

            if(StringUtils.isEmpty(companyName) || StringUtils.isEmpty(partnerName) || StringUtils.isEmpty(payway) || StringUtils.isEmpty( receiverName) ||StringUtils.isEmpty(receiverPhone)){
                throw new Exception("操作失败,公司名称,渠道名称,付款方式,联系人姓名,联系电话不能为空!");
            }
            //根据渠道商名称查询渠道代码
            String partnerCode = partnerFinanceRelationService.queryPartnerCodeByName(partnerName.replaceAll("\\s+", ""));
            if(StringUtils.isEmpty(partnerCode))throw new Exception("操作失败,渠道商:"+partnerName+"不存在或未入网");
            vo.setPartnerCode(partnerCode);
            //转换中文付款方式为字典
            transforPayWay(vo, payway);
            //转换合格证是否随车为字典
            transforIsOnVehicle(vo, isOnVehicle);
            saveOrUpdate(vo);
        }
    }

    /**
     * 转换合格证是否随车为字典
     * @param vo
     * @param isOnVehicle
     */
    private void transforIsOnVehicle(PartnerFinanceRelationVO vo, String isOnVehicle) {
        switch (isOnVehicle){
            case "Y":
                vo.setIsOnVehicle("Y");
                break;
            case "N":
                vo.setIsOnVehicle("N");
                break;
            case "是":
                vo.setIsOnVehicle("Y");
                break;
            case "否":
                vo.setIsOnVehicle("N");
                break;
            default:
                vo.setIsOnVehicle("Y");
        }
    }

    /**
     * 转换中文付款方式为字典
     * @param vo
     * @param payway
     */
    private void transforPayWay(PartnerFinanceRelationVO vo, String payway) {
        switch (payway){
            case DictConstant.CN_PAYWAYS_SFCD:
                vo.setPayway("1");
                break;
            case DictConstant.CN_PAYWAYS_AFC:
                vo.setPayway("2");
                break;
            case DictConstant.CN_PAYWAYS_OTHER:
                vo.setPayway("3");
                break;
            default:
                vo.setPayway("1");//如果不在这三种付款方式内,则默认给一个三方承兑的方式
        }
    }

    /**
     * 根据渠道代码查询融资机构信息
     * @param partnerCode
     * @return
     * @throws Exception
     */
    @Override
    public List<PartnerFinanceRelationInfoVO> listByPartnerCode(String partnerCode) throws Exception {
        QueryWrapper<PartnerFinanceRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARTNER_CODE",partnerCode);
        queryWrapper.eq("IS_ON_USE",1);
        List<PartnerFinanceRelation> list = partnerFinanceRelationService.list(queryWrapper);
        if(list == null || list.size() <= 0)throw new Exception("操作失败,无法根据渠道代码:"+partnerCode+"找到对应记录!");
        List<PartnerFinanceRelationInfoVO> list1 = BeanTools.copyListToList(list, PartnerFinanceRelationInfoVO.class);
        return list1;
    }

    /**
     * 根据VIN查询融资机构信息
     * @param vin
     * @return
     * @throws Exception
     */
    @Override
    public List<PartnerFinanceRelationInfoVO> listByVin(String vin) throws Exception {
        return partnerFinanceRelationService.listByVin(vin);
    }

    /**
     * 获取有效付款方式
     * @return
     */
    @Override
    public List<PartnerFinanceRelationAccountVO> accountType() {
        return partnerFinanceRelationService.accountType();
    }
}
